package edu.xmut.singleton.hungry;

/**
 * @author 朔风
 * @date 2024-02-16 12:52
 */
public class SingletonTest01 {
    public static void main(String[] args) {
        SingletonHungry instance1 = SingletonHungry.getInstance();
        SingletonHungry instance2 = SingletonHungry.getInstance();

        System.out.println(instance1 == instance2);
        System.out.println(instance1.hashCode() == instance2.hashCode());
    }
}

/**
 * 饿汉式单利模式（静态变量）
 */
class SingletonHungry{
    // 1.私有的构造器
    private SingletonHungry(){}

    // 2.静态变量实例
    private static SingletonHungry instance = new SingletonHungry();

    // 3.提供一个类方法返回实例对象
    public static SingletonHungry getInstance() {
        return instance;
    }


}
